//添加用户
const add = (model, data, ctx) => (model.create(data).then((rel) => {
    if (rel) {
        ctx.body = {
            code: 200,
            msg: '添加成功',
            data: rel
        }
    } else {
        ctx.body = {
            code: 300,
            msg: '添加失败'
        }
    }
}).catch(err => {
    ctx.body = {
        code: 400,
        msg: '添加时服务器异常'
    }
    console.log(err);
}))
//修改用户
const update = (model, params, ctx) => (model.updateOne(
    { _id: params._id },
    {
        username: params.username,
        pwd: params.pwd
    }
).then(rel => {
    ctx.body = {
        reslut: rel
    }
}).catch(err => {
    ctx.body = {
        code: 300,
        msg: '修改时出现异常'
    }
}))
// 删除用户
const del = (model, _id, ctx) => (model.findOneAndDelete({ _id }).then(rel => {
    ctx.body = {
        reslut: rel
    }
}).catch(err => {
    ctx.body = {
        code: 300,
        msg: '删除时出现异常'
    }
}))
//查询所有用户
const find = (model, where, ctx) => (model.find(where).then(rel => {
    ctx.body = {
        reslut: rel
    }
}).catch(err => {
    ctx.body = {
        code: 400,
        msa: "查询所有用户时出现异常"
    }
}))
//查询单个用户
const findOne = (model, _id, ctx) => (model.findOne({ _id }).then(rel => {
    ctx.body = {
        reslut: rel
    }
}).catch(err => {
    ctx.body = {
        code: 400,
        msa: "查询单个用户时出现异常"
    }
}))


module.exports = {
    add,
    update,
    del,
    find,
    findOne
}